﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace MangaRipper.Core
{
    [Serializable]
    class ChapterMangaGo : ChapterBase
    {
        public ChapterMangaGo(string name, Uri address) : base(name, address) { }

        protected override List<Uri> ParsePageAddresses(string html)
        {
            var list = new List<Uri>();
            list.Add(Address);
            Regex reg = new Regex(@"<li><a href='(?<Value>[^<]+)'>(?<Text>[^<]+)</a></li>", 
                RegexOptions.Multiline);
            
            MatchCollection matches = reg.Matches(html);

            foreach (Match match in matches)
            {
                var value = new Uri(Address, match.Groups["Value"].Value);
                if (!list.Contains(value))
                {
                    list.Add(value);
                }
            }

            return list;
        }

        protected override List<Uri> ParseImageAddresses(string html)
        {
            var list = new List<Uri>();
            Regex reg = new Regex(@"<img id=""(?<Useless>[^<]+)"" class=""(?<Useless>[^<]+)"" style=""margin:0 auto;display:block;max-width:100%;min-width:800px;text-align:center;"" border=""0"" alt=""(?<Useless>[^<]+)"" src=""(?<Value>[^<]+)"" onerror=",
                RegexOptions.IgnoreCase);
            MatchCollection matches = reg.Matches(html);

            foreach (Match match in matches)
            {
                var value = new Uri(Address, match.Groups["Value"].Value.Trim());
                list.Add(value);
            }

            return list;
        }
    }
}
